package cn.seaboot.admin.user.web;

import cn.seaboot.admin.logger.bean.LogType;
import cn.seaboot.admin.logger.manager.ActionLogger;
import cn.seaboot.admin.mvc.Pagination;
import cn.seaboot.admin.mvc.argument.Params;
import cn.seaboot.admin.user.bean.User;
import cn.seaboot.admin.user.bean.UserDetail;
import cn.seaboot.admin.user.context.UserContext;
import cn.seaboot.admin.user.service.UserService;
import cn.seaboot.commons.core.CommonUtils;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 强制：任何返回前端的用户信息，密码必须置空
 *
 * @author Mr.css on 2018/7/18.
 */
@Controller
@RequestMapping("sys/user")
@Tag(name = "用户 [t_sys_user] 管理")
public class UserCtrl {

    @Resource
    private UserService userService;

    @Resource
    private UserContext userContext;

    /**
     * 判断用户是否存在
     *
     * @param username 账号
     * @param password 密码
     * @return affected rows
     */
    @ResponseBody
    @Operation(description = "注册账号")
    @Parameters({
            @Parameter(name = "username", description = "账号"),
            @Parameter(name = "password", description = "密码")
    })
    @RequestMapping(value = "register", method = RequestMethod.POST)
    public int register(@Params String username, @Params String password) {
        return userService.register(username, password);
    }

    /**
     * 重置密码
     *
     * @param account 账号
     * @return affected rows
     */
    @ResponseBody
    @Operation(description = "重置账号密码")
    @RequestMapping(value = "password/reset", method = RequestMethod.POST)
    @ActionLogger(type = LogType.USER, operation = "重置${account}账户密码")
    @Parameters({@Parameter(name = "account", description = "账号")})
    public int resetPassword(@Params String account) {
        return userService.resetPassword(account);
    }


    /**
     * 重置密码
     *
     * @param oldPassword 旧密码
     * @param newPassword 新密码
     * @return affected rows
     */
    @ResponseBody
    @Operation(description = "更换账号密码", parameters = {
            @Parameter(name = "oldPassword", description = "旧密码"),
            @Parameter(name = "newPassword", description = "新密码")})
    @RequestMapping(value = "password/change", method = RequestMethod.POST)
    @ActionLogger(type = LogType.USER,
            operation = "重置${account}账户密码")
    public int changePassword(String oldPassword, String newPassword) {
        return userService.changePassword(oldPassword, newPassword);
    }


    /**
     * 更换分组
     *
     * @param account 账号
     * @param groupId 部门ID
     * @return affected rows
     */
    @ResponseBody
    @Operation(description = "分配账号分组")
    @Parameters({
            @Parameter(name = "account", description = "账号"),
            @Parameter(name = "groupId", description = "部门ID")
    })
    @RequestMapping(value = "group", method = RequestMethod.POST)
    @ActionLogger(type = LogType.USER,
            operation = "更换${account}分组至${groupId} - ${groupName}")
    public int updateGroupId(@Params String account, @Params String groupId) {
        return userService.updateGroupId(account, groupId);
    }

    /**
     * 启用账号
     *
     * @param account 账号
     * @return affected rows
     */
    @ResponseBody
    @Operation(description = "启用账号")
    @Parameters({@Parameter(name = "account", description = "账号")})
    @RequestMapping(value = "on", method = RequestMethod.POST)
    @ActionLogger(type = LogType.USER, operation = "启用账号：${account}")
    public int turnOn(@Params String account) {
        return userService.turnOn(account);
    }

    /**
     * 冻结账号
     *
     * @param account 账号
     * @return affected rows
     */
    @ResponseBody
    @Operation(description = "禁用账号")
    @Parameters({@Parameter(name = "account", description = "账号")})
    @RequestMapping(value = "off", method = RequestMethod.POST)
    @ActionLogger(type = LogType.USER, operation = "冻结账号：${account}")
    public int turnOff(@Params String account) {
        return userService.turnOff(account);
    }

    /**
     * 用户详细信息
     *
     * @return pojo
     */
    @ResponseBody
    @Operation(description = "查询用户信息")
    @Parameters({@Parameter(name = "account", description = "账号")})
    @RequestMapping(value = "info", method = RequestMethod.GET)
    public User userInfo(String account) {
        if (CommonUtils.isEmpty(account)) {
            account = userContext.getUser().getAccount();
        }
        return userService.queryByAccount(account);
    }

    /**
     * 用户详细信息
     *
     * @return pojo
     */
    @ResponseBody
    @Operation(description = "修改用户信息")
    @RequestMapping(value = "info", method = RequestMethod.PUT)
    public int update(User user) {
        return userService.update(user);
    }

    /**
     * 用户详细信息
     *
     * @return -
     */
    @ResponseBody
    @Operation(description = "查询用户详细信息")
    @Parameters({@Parameter(name = "account", description = "账号")})
    @RequestMapping(value = "detail", method = RequestMethod.GET)
    public UserDetail userDetail(String account) {
        if (CommonUtils.isEmpty(account)) {
            account = userContext.getUser().getAccount();
        }
        return userService.queryUserDetail(account);
    }

    /**
     * 查询列表
     *
     * @param params 参数
     * @return list
     */
    @ResponseBody
    @Operation(description = "用户 [t_sys_user] 列表查询")
    @Parameters({
            @Parameter(name = "page", description = "页号"),
            @Parameter(name = "limit", description = "行数")})
    @RequestMapping(value = "page", method = RequestMethod.GET)
    public PageInfo<User> queryPage(@RequestParam Map<String, Object> params) {
        return Pagination.apply(params, map-> userService.queryList(map));
    }

    /**
     * 查询列表
     *
     * @param params 参数
     * @return list
     */
    @ResponseBody
    @Operation(description = "用户 [t_sys_user] 查询机构用户")
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public List<User> queryList(@RequestParam Map<String, Object> params) {
        return userService.queryList(params);
    }
}
